public class PrimeNumbers {
    public static void main(String[] args) {
        int count = 0;
        int limit = 20000;
        
        long startTime = System.currentTimeMillis();
        
        for (int i = 2; i <= limit; i++) {
            if (isPrime(i)) {
                System.out.printf("%-6d", i);
                count++;
                if (count % 5 == 0) {
                    System.out.println();
                }
            }
        }
        
        long endTime = System.currentTimeMillis();
        System.out.println("\n总素数个数: " + count);
        System.out.println("耗时: " + (endTime - startTime) + " ms");
    }
    
    // 判断素数的函数
    public static boolean isPrime(int n) {
        if (n <= 1) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;
        
        // 只需检查到√n，且只检查奇数
        for (int i = 3; i * i <= n; i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}